﻿using System;
using System.Reflection;
using System.Runtime.Intrinsics.Arm;

namespace iTeamyou.LTS.Core.Utils
{
	public class Comparator
	{
        /// <summary>
        /// 实体差异比较器
        /// </summary>
        /// <param name="source">源版本实体</param>
        /// <param name="current">当前版本实体</param>
        /// <returns>true 存在变更 false 未变更</returns>
        public bool DifferenceEntityComparison<T>(T source, T current, List<string>? exclude = null) where T : class
        {
            if (exclude == null || exclude.Any())
                exclude = new List<string>() { "Id", "CreatedOn", "CreatedBy", "LastModifiedOn", "LastModifiedBy", "IsPublish", "RefFlowId", "Product", "Manufacturer", "Supplier", "Batch" };
            Type t1 = source.GetType();
            Type t2 = current.GetType();
            PropertyInfo[] property1 = t1.GetProperties();

            foreach (PropertyInfo p in property1)
            {
                string name = p.Name;
                if (exclude.Contains(name)) { continue; }
                string value1 = p.GetValue(source, null)?.ToString();
                string value2 = t2.GetProperty(name)?.GetValue(current, null)?.ToString();
                if (value1 != value2)
                {
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 集合差异比较器,比较两个实体集合值是否一样
        /// </summary>
        /// <param name="source">源版本实体集合</param>
        /// <param name="current">当前版本实体集合</param>
        /// <returns>true 存在变更 false 未变更</returns>
        public bool DifferenceComparison<T>(List<T> source, List<T> current) where T : class
        {
            //排除主键和基础字段
            List<string> exclude = new List<string>() { "Id", "CreatedOn", "CreatedBy", "LastModifiedOn", "LastModifiedBy", "IsPublish", "RefFlowId", "Product", "Manufacturer", "Supplier", "Batch" };
            if (source.Count != current.Count) { return true; }
            for (int i = 0; i < source.Count; i++)
            {
                bool flag = DifferenceEntityComparison(source[i], current[i], exclude);
                if (flag) { return flag; }
            }
            return false;
        }
        /// <summary>
        /// 将实体2的值动态赋值给实体1(名称一样的属性进行赋值)
        /// </summary>
        /// <param name="model1">实体1</param>
        /// <param name="model2">实体2</param>
        /// <returns>赋值后的model1</returns>
        protected static T1 BindModelValue<T1, T2>(T1 model1, T2 model2) where T1 : class where T2 : class
        {
            Type t1 = model1.GetType();
            Type t2 = model2.GetType();
            PropertyInfo[] property2 = t2.GetProperties();
            //排除主键
            List<string> exclude = new List<string>() { "Id" };
            foreach (PropertyInfo p in property2)
            {
                if (exclude.Contains(p.Name)) { continue; }
                t1.GetProperty(p.Name)?.SetValue(model1, p.GetValue(model2, null));
            }
            return model1;
        }
    }
}

